using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using ShopForWeb.Dtos.ViewModel;
using ShopForWeb.Model.Basic;
using ShopForWeb.Model.Info;
using ShopForWeb.Services;
using ShopForWeb.UI.Models;
using ShopForWeb.UI.Utils.Extensions;
using ShopForWeb.UI.Utils.Filters;

namespace ShopForWeb.UI.Controllers
{
    [Route("[controller]")]
    [UserAuthroize]
    [UserInfo]
    public class OrderController : Controller
    {
        private readonly ILogger<OrderController> _logger;
        private OrderService _orderService;
        public OrderController(ILogger<OrderController> logger, OrderService orderService)
        {
            _logger = logger;
            _orderService = orderService;
        }

        public async Task<IActionResult> Index([FromQuery] int page = 1, [FromQuery] int size = 16, [FromQuery] int statusCode = 0)
        {
            var user = this.GetUser();
            var result = await _orderService.GetPageAsync(page, size, user?.Phone ?? "", statusCode);
            int total = await _orderService.GetTotalAsync(user?.Phone ?? "", statusCode);
            ViewBag.OrderList = result;
            ViewBag.PageBar = new PageBarView(page, size, total);
            ViewBag.StatusCode = statusCode;
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> AddNewAsync([FromBody] OrderInfo _info)
        {
            if (_info != null)
            {
                var result = await _orderService.AddNewAsync(_info.Order, _info.Details);
                if (result > 0)
                    return Json(new ResponseViewModel()
                    {
                        Code = 200,
                        Message = "订单已生成"
                    }
                    );
                else
                    return Json(new ResponseViewModel()
                    {
                        Code = 200,
                        Message = "订单生成失败"
                    }
                   );
            }
            else return BadRequest("对象为空");

        }
        [HttpDelete("{orderId}")]
        public async Task<IActionResult> RemoveAsync([FromRoute] String orderId)
        {
            int result = await _orderService.RemoveAsync(orderId);
            if (result == 1)
                return Json(new ResponseViewModel()
                {
                    Code = 200,
                    Message = "订单删除成功"
                }
                );
            else
                return Json(new ResponseViewModel()
                {
                    Code = 500,
                    Message = "订单删除失败"
                }
               );
        }
        [HttpGet("{orderId}/detail")]
        public async Task<IActionResult> GetOrderDetailAsync(string orderId, [FromServices] OrderDetailService orderDetallservice)
        {
            var result = await orderDetallservice.GetByOrderIdAsync(orderId);
            return Json(new
            {
                Code = 200,
                Data = result
            });

        }

        
    }
}